import { formatDate, formatTimestamp } from '@/utils/formatTime';
/**
 * @deprecated 深拷贝
 * @param {*} source
 * @returns
 */
export const deepClone = (source) => {
	if (source === null || typeof source !== 'object') return source;
	const target = Array.isArray(source) ? [] : {};
	for (const key in source) {
		if (Object.prototype.hasOwnProperty.call(source, key)) {
			target[key] = deepClone(source[key]);
		}
	}
	return target;
};

/**
 * @description  延迟执行
 * @param {*} ms 延迟执行毫秒
 * @returns
 */
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

/**
 * @description 生成随机字符串
 * @param {*} length
 * @returns
 */
export const generateRandomString = (length) => {
	let result = '';
	const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	const charactersLength = characters.length;
	for (let i = 0; i < length; i++) {
		result += characters.charAt(Math.floor(Math.random() * charactersLength));
	}
	return result;
};

/**
 * @description 送货单、确认单数据处理
 * @param {*} res 订单详情响应体
 * @param {*} Tcolor 牙位颜色配置
 * @param {*} Yrequire 设计要求配置
 * @returns Processed items array
 */
export const updatedItems = (res, Tcolor, Yrequire) => {
	return res?.items.map((item) => processItem(res, item, Tcolor, Yrequire));
};

// 获取牙色配置
const getColor = ({ b16c, b26c, jbys, szybs }, Tcolor) => {
	const color = Tcolor.value;
	return [color[b16c], color[b26c], color[jbys], color[szybs]];
};

// 牙位
const toothData = (item) => [...(item?.multiple_crown_text ? item?.multiple_crown_text.split(',') : []), ...(item?.single_crown_text ? item?.single_crown_text.split(',') : [])].filter(Boolean);

// 订单属性
const getOrderAttributes = (data) => {
	const state = {
		1: '新做',
		2: '返修',
		3: '重做'
	};
	return state[data.type_status];
};

/**
 * @description Common item processing function
 * @param {*} res 订单详情响应体
 * @param {*} item 单个项目数据
 * @param {*} Tcolor 牙位颜色配置
 * @param {*} Yrequire 设计要求配置
 * @returns Processed item object
 */
const processItem = (res, item, Tcolor, Yrequire) => {
	const tooth = toothData(item)
		.filter((item) => item)
		.sort((a, b) => a - b);

	//处理附件类型
	const getFittings = () => {
		if (!res.fittings) return '';
		return res.fittings
			.filter((item) => item.num > 0)
			.map((item) => {
				let str = item.fittinfo.title + ': ' + item.num;
				return str;
			})
			.join(',');
	};

	return {
		// 订单编号
		order_sn: res.order_sn || '',
		// 医院名称
		hospital: res?.hospitalson?.title || '',
		// 医生姓名
		docter: res.docuser?.nickname || '',
		// 患者姓名
		name: res.name || '',
		// 牙位
		tooth: tooth.join(',') || '',
		//一级分类
		parent: item.product?.product_type?.parent?.title || '',
		// 二级分类
		product_type: item.product?.product_type?.title || '',
		// 产品名称
		productName: item.product.title || '',
		// 牙位数量
		num: item.num || 0,
		// 牙色
		color: getColor(item, Tcolor).filter(Boolean).join(',') || '',
		// 设计要求
		// requirements: getRequire(item, Yrequire).filter(Boolean).join(',') || '',
		design_req: res?.design_req || '',
		// 备注
		qtyq: res?.remark || '',
		// 收模日期
		createtime: formatDate(res.createtime) || '',
		// 订单属性
		orderAttributes: getOrderAttributes(res) || '',
		// 地区
		area: res.hospital?.area?.title || '',
		// 机构名称
		institutionName: res?.hospital?.title || '',
		// 锆块码
		block_code: res.block_code || '',
		// 交付时间
		// delivery_time: addDay(item?.product?.production_days) || '',
		delivery_time: formatDate(new Date().getTime()) || '',
		// 接收日期
		receivetime: (res.receivetime && formatTimestamp(res.receivetime)) || '',
		//固定习惯
		habits: res?.hospital?.habits || '',
		//代收款 当结算状态为3时才赋值打印金额 默认0
		amount: res?.hospital?.jiesuan_status == 3 && res?.total_price != 0 ? res?.total_price : '无',
		//盒号
		box_num: res?.box_num || '',
		//试戴类型
		try_str: res?.try_str_info || '',
		//附件信息
		attachmentRequire: getFittings()
	};
};
